package com.chuanghai.light.tcp;

import com.chuanghai.light.entity.TimeStruct;
import com.chuanghai.light.entity.TimeStructList;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class LightControlThread implements Runnable{
    private Thread t;
    private String threadName;
    private MysqlTool mysqlTool;

    LightControlThread( String name) {
        threadName = name;
    }


    @Override
    public void run() {
        try {
           while (true){
               TCPServer tcpServer = new TCPServer();
               HashMap<String,TimeStructList> timeStructListMap = mysqlTool.getAllTime();
               if(timeStructListMap!=null)
               {
                   // System.out.println("timeStructList: " + timeStructListMap.size());
                   TimeStruct nowTimeStruct = getNowTimeStruct();
                   for (HashMap.Entry<String, TimeStructList> entry : timeStructListMap.entrySet()) {
                       //System.out.println("key = " + entry.getKey());
                       TimeStructList timeStructList = entry.getValue();
                       for(int i=0;i<timeStructList.onTime.size();i++)
                       {
                           //System.out.println( "   onTime:"+timeStructList.onTime.get(i).hour + ":" + timeStructList.onTime.get(i).min);
                           //System.out.println( "   offTime:"+timeStructList.offTime.get(i).hour + ":" + timeStructList.offTime.get(i).min);
                           if(timeStructList.onTime.get(i).hour==nowTimeStruct.hour && timeStructList.onTime.get(i).min==nowTimeStruct.min)  // 开
                           {
                               tcpServer.setLightState(entry.getKey(), true);
                           }
                           if(timeStructList.offTime.get(i).hour==nowTimeStruct.hour && timeStructList.offTime.get(i).min==nowTimeStruct.min)  // 关
                           {
                               tcpServer.setLightState(entry.getKey(), false);
                           }
                       }
                   }
               }
                Thread.sleep(25000);  // 25秒判断一次，是否需要控制,也就是说每个时间点最多会发送3次控制命令
            }
        }catch (InterruptedException e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
    }


    public void start () {
        System.out.println("Starting " +  threadName );

        if (mysqlTool == null) {
            mysqlTool= new MysqlTool();
        }
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }

    /**
     * 获取当前时间
     * @return
     */
    public TimeStruct getNowTimeStruct(){
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("HH:mm");
        Date date = new Date();// 获取当前时间
        String nowTimeList[] = sdf.format(date).split(":");
        TimeStruct nowTimeStruct = new TimeStruct();
        nowTimeStruct.hour = Integer.valueOf(nowTimeList[0]);
        nowTimeStruct.min = Integer.valueOf(nowTimeList[1]);
        //System.out.println("现在时间：" + nowTimeStruct.hour+":"+nowTimeStruct.min);
        return nowTimeStruct;
    }

}
